
// 模拟数据
const mockData = {
    user: {
        name: "张三",
        age: 25,
        gender: "男",
        contact: "138****5678",
        emergencyContact: "李四 (139****1234)",
        lastActive: "2分钟前"
    },
    riskScores: {
        depression: 85,
        anxiety: 78,
        suicide: 72,
        sleep: 60,
        socialSupport: 40
    },
    moodTrend: [7, 5, 3, 2, 1],
    interventionHistory: [
        { date: "2025-01-15", type: "电话咨询", effect: 6 },
        { date: "2025-01-20", type: "面对面咨询", effect: 7 },
        { date: "2025-01-25", type: "药物调整", effect: 5 }
    ]
};

// 初始化页面
function initializePage() {
    updateUserInfo();
    drawCharts();
    updateRiskLevel();
}

// 更新用户信息
function updateUserInfo() {
    document.getElementById('userName').textContent = mockData.user.name;
    document.getElementById('userAge').textContent = mockData.user.age + '岁';
    document.getElementById('userGender').textContent = mockData.user.gender;
    document.getElementById('userContact').textContent = mockData.user.contact;
    document.getElementById('emergencyContact').textContent = mockData.user.emergencyContact;
    document.getElementById('lastActive').textContent = mockData.user.lastActive;
}

// 更新风险等级
function updateRiskLevel() {
    const avgRisk = Object.values(mockData.riskScores).reduce((a, b) => a + b, 0) / Object.values(mockData.riskScores).length;
    const levelElement = document.getElementById('crisisLevel');

    if (avgRisk >= 70) {
        levelElement.textContent = '高风险';
        levelElement.className = 'crisis-level crisis-high';
    } else if (avgRisk >= 40) {
        levelElement.textContent = '中风险';
        levelElement.className = 'crisis-level crisis-medium';
    } else {
        levelElement.textContent = '低风险';
        levelElement.className = 'crisis-level crisis-low';
    }
}

// 绘制图表
function drawCharts() {
    drawTrendChart();
    drawRiskChart();
    drawInterventionChart();
}

// 绘制趋势图
function drawTrendChart() {
    const canvas = document.getElementById('trendCanvas');
    const ctx = canvas.getContext('2d');

    // 清空画布
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // 设置样式
    ctx.strokeStyle = '#e53e3e';
    ctx.lineWidth = 3;
    ctx.fillStyle = 'rgba(229, 62, 62, 0.2)';

    // 绘制情绪趋势线
    const data = mockData.moodTrend;
    const stepX = canvas.width / (data.length - 1);
    const stepY = canvas.height / 10;

    ctx.beginPath();
    ctx.moveTo(0, canvas.height - data[0] * stepY);

    for (let i = 1; i < data.length; i++) {
        ctx.lineTo(i * stepX, canvas.height - data[i] * stepY);
    }

    ctx.stroke();

    // 填充区域
    ctx.lineTo(canvas.width, canvas.height);
    ctx.lineTo(0, canvas.height);
    ctx.closePath();
    ctx.fill();

    // 绘制数据点
    ctx.fillStyle = '#e53e3e';
    for (let i = 0; i < data.length; i++) {
        ctx.beginPath();
        ctx.arc(i * stepX, canvas.height - data[i] * stepY, 5, 0, 2 * Math.PI);
        ctx.fill();
    }

    // 添加标签
    ctx.fillStyle = 'white';
    ctx.font = '12px Arial';
    ctx.textAlign = 'center';
    for (let i = 0; i < data.length; i++) {
        ctx.fillText(data[i], i * stepX, canvas.height - data[i] * stepY + 4);
    }
}

// 绘制风险分布图
function drawRiskChart() {
    const canvas = document.getElementById('riskCanvas');
    const ctx = canvas.getContext('2d');

    ctx.clearRect(0, 0, canvas.width, canvas.height);

    const risks = Object.values(mockData.riskScores);
    const labels = ['抑郁', '焦虑', '自杀', '睡眠', '社会支持'];
    const colors = ['#e53e3e', '#dd6b20', '#c53030', '#ed8936', '#38a169'];

    const barWidth = canvas.width / risks.length;
    const maxHeight = canvas.height - 40;

    for (let i = 0; i < risks.length; i++) {
        const barHeight = (risks[i] / 100) * maxHeight;
        const x = i * barWidth + 10;
        const y = canvas.height - barHeight - 20;

        // 绘制柱状图
        ctx.fillStyle = colors[i];
        ctx.fillRect(x, y, barWidth - 20, barHeight);

        // 添加标签
        ctx.fillStyle = 'white';
        ctx.font = '12px Arial';
        ctx.textAlign = 'center';
        ctx.fillText(labels[i], x + (barWidth - 20) / 2, canvas.height - 5);
        ctx.fillText(risks[i], x + (barWidth - 20) / 2, y + barHeight / 2 + 4);
    }
}

// 绘制干预效果图
function drawInterventionChart() {
    const canvas = document.getElementById('interventionCanvas');
    const ctx = canvas.getContext('2d');

    ctx.clearRect(0, 0, canvas.width, canvas.height);

    const interventions = mockData.interventionHistory;
    const stepX = canvas.width / interventions.length;

    ctx.strokeStyle = '#4299e1';
    ctx.lineWidth = 2;
    ctx.fillStyle = 'rgba(66, 153, 225, 0.3)';

    ctx.beginPath();
    ctx.moveTo(0, canvas.height - interventions[0].effect * 30);

    for (let i = 1; i < interventions.length; i++) {
        ctx.lineTo(i * stepX, canvas.height - interventions[i].effect * 30);
    }

    ctx.stroke();

    // 绘制干预点
    ctx.fillStyle = '#4299e1';
    for (let i = 0; i < interventions.length; i++) {
        ctx.beginPath();
        ctx.arc(i * stepX, canvas.height - interventions[i].effect * 30, 6, 0, 2 * Math.PI);
        ctx.fill();

        // 添加标签
        ctx.fillStyle = 'white';
        ctx.font = '10px Arial';
        ctx.textAlign = 'center';
        ctx.fillText(interventions[i].type, i * stepX, canvas.height - 10);
    }
}

// 模态框功能
function openEmergencyModal() {
    document.getElementById('emergencyModal').style.display = 'block';
}

function closeEmergencyModal() {
    document.getElementById('emergencyModal').style.display = 'none';
}

function contactTherapist() {
    alert('正在连接专业心理咨询师...请稍候');
    // 这里可以添加实际的联系功能
}

function showResources() {
    alert('正在为您准备个性化的自助资源...');
    // 这里可以跳转到资源页面
}

// 页面加载完成后初始化
window.onload = function() {
    initializePage();

    // 模拟实时数据更新
    setInterval(function() {
        // 模拟数据变化
        const lastActive = document.getElementById('lastActive');
        const now = new Date();
        lastActive.textContent = Math.floor(Math.random() * 5) + 1 + '分钟前';
    }, 30000);
};

// 点击模态框外部关闭
window.onclick = function(event) {
    const modal = document.getElementById('emergencyModal');
    if (event.target === modal) {
        modal.style.display = 'none';
    }
};
